package com.example.xm.ui.own.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.xm.MainActivity;
import com.example.xm.R;
import com.example.xm.base.BaseActivity;
import com.example.xm.bean.OwnBean.UserBean;
import com.example.xm.interfaces.own.IOwn;
import com.example.xm.presenter.own.LoginPresenter;
import com.example.xm.utiles.SpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends BaseActivity<IOwn.LoginPersenter> implements IOwn.LoginView {

    @BindView(R.id.iv_finish_login)
    ImageView ivFinishLogin;
    @BindView(R.id.ll_login)
    RelativeLayout llLogin;
    @BindView(R.id.ll_head_login)
    LinearLayout llHeadLogin;
    @BindView(R.id.ll_login_ed)
    LinearLayout llLoginEd;
    @BindView(R.id.video_v)
    VideoView videoV;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.imv_pwd)
    ImageView imvPwd;
    @BindView(R.id.et_name_login1)
    EditText etNameLogin1;
    @BindView(R.id.et_pwd_login1)
    EditText etPwdLogin1;
    @BindView(R.id.btn_login1)
    Button btnLogin1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initVideo();
        initView();
        initListener();
    }

    @Override
    protected IOwn.LoginPersenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    private void initVideo() {
        //播放路径
        videoV.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.login));
        //播放
        videoV.start();
        //循环播放
        videoV.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                videoV.start();
            }
        });
    }

    @Override
    protected void initListener() {
        ivFinishLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });
        btnLogin1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = etNameLogin1.getText().toString();
                String pwd = etPwdLogin1.getText().toString();
                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pwd)) {
                    Map<String, String> map = new HashMap<>();
                    map.put("username", name);
                    map.put("password", pwd);
                    mPresenter.login(map);
                } else {
                    Toast.makeText(LoginActivity.this, "账号密码不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
        imvPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etPwdLogin1.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {//不可见
                    imvPwd.setImageResource(R.mipmap.yanjingbi);
                    etPwdLogin1.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                } else {
                    imvPwd.setImageResource(R.mipmap.yanjingkai);
                    etPwdLogin1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }
            }
        });
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String pwd = intent.getStringExtra("pwd");
        etNameLogin1.setText(name);
        etPwdLogin1.setText(pwd);
        etPwdLogin1.getBackground().setAlpha(220);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void loginReturn(UserBean userBean) {
        if (userBean.getErr() == 200) {
            //把登录成功以后的数据存入sp
            SpUtils.getInstance().setValue("token", userBean.getData().getToken());
            SpUtils.getInstance().setValue("username", userBean.getData().getUsername());
            SpUtils.getInstance().setValue("avater", userBean.getData().getAvater());
            SpUtils.getInstance().setValue("phone", userBean.getData().getPhone());
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, userBean.getErrmsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void registerReturn(UserBean userBean) {

    }

    @Override
    public void showTips(String tips) {

    }

    @Override
    public void showLoading(int visble) {

    }
}